package com.ww.controller;

import com.ww.common.resp.CommonResp;
import com.ww.req.MemberLoginReq;
import com.ww.req.MemberRegisterReq;
import com.ww.req.MemberSendCodeReq;
import com.ww.resp.MemberLoginResp;
import com.ww.services.MemberService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
    private static final Logger LOG= LoggerFactory.getLogger(MemberService.class);
    @GetMapping("/hello-member")
    public String hello(){ return  "hello world !!!!!";}

    @Resource
    private MemberService memberService;

    @GetMapping("/member/count")
    public CommonResp<Integer> count(){
        // 通过service调取count
        int count = memberService.Count();
        // 实例化CommonResp类 指定integer型
        CommonResp<Integer> CommonResp = new CommonResp<>();
        // 通过泛型接收值
        CommonResp.setContent(count);
        // 返回实体类
        return  CommonResp;
    }

    @PostMapping("/member/register")
    public  CommonResp<Long> register(@Valid MemberRegisterReq req){
        long register = memberService.register(req);
//        CommonResp<Long> CommonResp = new CommonResp<>();
//        CommonResp.setContent(register);
//        return  CommonResp;
        // CommonResp加了无参构造，所以上面三行可以用一行代码解决
        return new CommonResp<>(register);
    }

    @PostMapping("/member/send-code")
    public  CommonResp<Long> sendCode(@Valid @RequestBody MemberSendCodeReq req){
        memberService.sendCode(req);
        return new CommonResp<>();
    }

    @PostMapping("/member/login")
    public  CommonResp<MemberLoginResp> login(@Valid @RequestBody MemberLoginReq req){
        LOG.info(String.valueOf(req));
        MemberLoginResp resp = memberService.Login(req);
        return new CommonResp<>(resp);
    }

}
